function addLeave($scope, CertusFormAjax) {
    CertusFormAjax.ajax('initDepts',{},function (response) {
        var data = response.data;
        $scope.depts = data;
        if (data.length==1){
            $("#deptCode").attr("disabled","disabled");
        }
        $scope.deptCode = data[0].departmentCode;
    });

    CertusFormAjax.ajax('findAllVtType', {}, function (response) {
        $scope.type = response.data;
    });

    this.calcHours = function (days) {
        if(!$.isNumeric(days)){
            $("#vacationDay").val(0);
            return;
        }
        var vacationHour = $("#vacationHour").val();
        if (days == "") {
            return;
        }
        var totalHours = Number(days * 8) + Number(vacationHour);
        $("#totalHour").val(totalHours);
    };

    this.calcueDay = function (hours) {
        var vacationDay = $("#vacationDay").val();
        if (vacationDay == "") {
            $("#totalHour").val("");
            return;
        }
        var totalHours = Number(vacationDay * 8) + Number(hours);
        $("#totalHour").val(totalHours);
    };

    //系统计算两日期相差多少个小时
    this.getInervalHour=function(startDate, endDate,beginTime,endTime) {
        if (beginTime==endTime){
            return 0;
        }else{
            var d1 = new Date(startDate);
            var d2 = new Date(endDate);
            return parseInt(d2 - d1) / 1000 / 3600+24;
        }
    };

    $(".submit").on('click', function () {

        var telephone = $("#telephone").val();
        var comments = $("#comments").val();
        var vacationDay = $("#vacationDay").val();
        var vacationHour = $("#vacationHour").val();

        // 获取开始日期时间
        var startDay = $("#beginTime").val();
        var startTime = $("#start option:selected").val();
        var startDT = startDay + " " + startTime;
        // console.log("开始日期时间：" + startDT);

        // 获取结束日期时间
        var endDay = $("#endTime").val();
        var endTime = $("#end option:selected").val();
        var endDT = endDay + " " + endTime;

        if (startDay == "" || startDay == null || startDay == undefined) {
            layer.msg("开始时间不能为空！");
            return;
        }
        if (endDay == "" || endDay == null || endDay == undefined) {
            layer.msg("结束时间不能为空！");
            return;
        }
        var diffTHours = getInervalHour(startDT,endDT,startDay,endDay);
        var diffDay = parseInt(diffTHours/24);
        // var diffChours = diffTHours%24;

        // if (diffDay!=vacationDay){
        //     layer.msg("请假总时长填写不正确，请重新填写！正确日期为："
        //         +diffDay+"天");
        //     return;
        // }
        if (telephone == "" || telephone == null || telephone == undefined) {
            layer.msg("紧急联系方式不能为空！");
            return;
        }
        if (comments.trim() == "" || comments == null || comments == undefined) {
            layer.msg("请假原因不能为空！");
            return;
        }

        // 转换开始标准时间
        var start = new Date(startDT);
        // console.log("开始标准时间：" + start);
        // 转换结束标准时间
        var end = new Date(endDT);
        // console.log("结束标准时间：" + end);
        // 当前日期及时间
        var toDay = new Date();
        // console.log("当前标准时间：" + toDay);

        // if (Date.parse(start) < Date.parse(toDay)) {
        //     layer.msg("开始时间不能小于当前时间！");
        //     return;
        // }
        // if (Date.parse(end) < Date.parse(toDay)) {
        //     layer.msg("结束时间不能小于当前时间！");
        //     return;
        // }
        if (Date.parse(end) == Date.parse(start)) {
            layer.msg("结束时间不能等于开始时间！");
            return;
        }
        if (Date.parse(end) < Date.parse(start)) {
            layer.msg("结束时间不能小于开始时间！");
            return;
        }

        // 截取时间保留分钟
        var starMinutes = Number(startTime.substring(3, 5));
        var endMinutes = Number(endTime.substring(3, 5));
        if (starMinutes != endMinutes) {
            layer.msg("请假最小时长计量单位：一小时");
            return;
        }
        if ($("#totalHour").val() == "0") {
            layer.msg("请假总时长不能为0");
            return;
        }

        var arr = $("form").serializeArray();
        var data = {};
        for (var i = 0; i < arr.length; i++) {
            data[arr[i].name] = arr[i].value;
        };
        data.totalHour = $("#totalHour").val();
        data.deptCode = $("#deptCode").val();

        // 开启
        var index = layer.load(1);
        CertusFormAjax.ajax('addLeave', {
            "param": JSON.stringify(data)
        }, function () {
            //关闭
            layer.close(index);
            layer.msg("申请成功");
            window.history.back()
        });
    });
}
function showType(code) {
    var type="";
    if (code==1){
        type="年假及病事假";
    }else if (code==4){
        type="档假";
    }else if (code==5){
        type="婚假";
    }else if (code==6){
        type="产检假";
    }else if (code==7){
        type="产假";
    }else if (code==8){
        type="陪产假";
    }else if (code==9){
        type="哺乳假";
    }else if (code==10){
        type="节育假";
    }else if (code==11){
        type="丧假";
    }else if (code==12){
        type="工伤假";
    }
    return type;
}
//查看详情
function findLdetail($scope, CertusFormAjax, $routeParams) {

    CertusFormAjax.ajax("findVtlists", {
        "processId": $routeParams.processId
    }, function (result) {
        var data = result.data;
        $scope.vacationType = showType(data.vacationType);
        $scope.createUser = data.createUser;
        $scope.beginTime = data.beginTime;
        $scope.start = data.start;
        $scope.endTime = data.endTime;
        $scope.end = data.end;
        $scope.vacationDay = data.vacationDay;
        $scope.totalHour = data.totalHour;
        $scope.telephone = data.telephone;
        $scope.comments = data.comments;
        $scope.opinionMsg = data.opinionMsg;
    });

    CertusFormAjax.ajax("findSpjls", {
        "processId": $routeParams.processId
    }, function (result) {
        var data = result.data;
        var html = "";
        var dlength = data.length;
        for (var i = 0; i < dlength; i++) {
            html+="<li><span>"+data[i].createUser+"</span><span>"+data[i].createTime+"</span>";
            if (data[i].nodeFlag==1){
                html+="<span>发起申请</span>";
            }else if (data[i].nodeFlag==2){
                html+="<span>退回，审批意见："+data[i].opinionMsg+"</span>";
            }else if (data[i].nodeFlag == 3){
                html+="<span>拒绝，审批意见："+data[i].opinionMsg+"</span>";
            }else if (data[i].nodeFlag==4){
                html+="<span>同意，审批意见："+data[i].opinionMsg+"</span>";
            }else{
                html+="<span>审批通过，审批意见："+data[i].opinionMsg+"</span>";
            }
            html+="</li>";
        };
        $("#containerUl").append(html);
    });
}